package cz.proks.easyhelpdesk.gwt.activity.base;

import java.util.Collection;

import com.google.gwt.place.shared.PlaceHistoryMapper;

import cz.proks.easyhelpdesk.api.model.Helpdesk;
import cz.proks.easyhelpdesk.gwt.ClientFactory;
import cz.proks.easyhelpdesk.gwt.Globals;
import cz.proks.easyhelpdesk.gwt.event.FilterChangeEvent;
import cz.proks.easyhelpdesk.gwt.event.HelpdeskChangeEvent;
import cz.proks.easyhelpdesk.gwt.place.HelpdeskMainPlace;
import cz.proks.easyhelpdesk.gwt.place.NewTicketPlace;
import cz.proks.easyhelpdesk.gwt.place.SettingsPlace;
import cz.proks.easyhelpdesk.gwt.place.base.RestrictedHelpdeskPlace;
import cz.proks.easyhelpdesk.gwt.ui.base.RestrictedHelpdeskView;

public abstract class RestrictedHelpdeskActivity<ThePlace extends RestrictedHelpdeskPlace> extends RestrictedActivity<ThePlace> implements RestrictedActivity.InitDelegator, HelpdeskChangeEvent.Handler, FilterChangeEvent.Handler {
    private InitDelegator initDelegator;    

    public RestrictedHelpdeskActivity(ThePlace place, ClientFactory clientFactory, PlaceHistoryMapper placeMapper) {
        super(place, clientFactory, placeMapper);
        super.setInitDelegator(this);               
    }        

    @Override
    protected abstract RestrictedHelpdeskView getView();
    
    @Override
    protected boolean hasAccess() {     
        return super.hasAccess() && 
               Globals.getCurrentHelpdesk() != null && 
               Globals.getCurrentHelpdeskMember() != null;
    }
    
    @Override
    protected void setInitDelegator(InitDelegator initDelegator) {
        this.initDelegator = initDelegator;
    }   

    @Override
    public void onInit() {
        Collection<Helpdesk> helpdesks = Globals.getHelpdesks();
        
        Long selectedHelpdeskId = getPlace().getHelpdeskId();                
        
        Helpdesk selectedHelpdesk = Globals.getHelpdesk(selectedHelpdeskId);        
        Globals.setCurrentHelpdesk(selectedHelpdesk);
        Globals.setCurrentHelpdeskMember(Globals.getHelpdeskMemberFor(selectedHelpdesk, Globals.getCurrentUser()));
        
        getView().setHelpdesks(helpdesks);                                   
        getView().setSelectedHelpdesk(selectedHelpdesk);
        getView().addHelpdeskChangeHandler(this);
        getView().addFilterChangeHandler(this);     
        getView().showSettingsButton(Globals.isHelpdeskOwner());
        
        getView().setCreateTicketButtonTargetToken(getPlaceMapper().getToken(new NewTicketPlace(selectedHelpdeskId)));
        getView().setSettingsButtonTargetToken(getPlaceMapper().getToken(new SettingsPlace(selectedHelpdeskId)));
        
        if (initDelegator != null)
            initDelegator.onInit();
    }
        
    @Override
    public void onFilterChange(FilterChangeEvent event) {
        
    }

    @Override
    public void onHelpdeskChange(HelpdeskChangeEvent event) {
        Helpdesk helpdesk = event.getHelpdesk();
        getClientFactory().getPlaceController().goTo(new HelpdeskMainPlace(helpdesk.getId()));
    }            
}
